package com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.controller;

import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.entity.AreaRatio;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.entity.AssessmentVulnerability;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.param.CalculateVO;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.param.EvaluateParam;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.param.ModelParam;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.param.SysDataVersionPageParam;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.service.CasualtiesService;
import com.css.fxfzxczh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年07月26日  14:17
 * 人员伤亡
 */
@Slf4j
@RestController
@RequestMapping("/casualties")
public class CasualtiesController {

    @Resource
    CasualtiesService casualtiesService;
    /**
     * 人员伤亡评估计算
     */
    @PostMapping("/evaluate")
    public RestResponse evaluate(@RequestBody EvaluateParam param) throws Exception {
        System.out.println("人员伤亡评估第3步开始");
        Long start = System.currentTimeMillis();
        if(StringUtils.isBlank(param.getModelId())){
            throw new RuntimeException("评估失败,请选择评估模型");
        }
        if(StringUtils.isBlank(param.getXsemId())){
            throw new RuntimeException("评估失败,请选择要评估的地震事件");
        }
        casualtiesService.evaluate(param);
        System.out.println("人员伤亡评估第3步结束，花费时间"+((System.currentTimeMillis()-start)/1000));
        return ResponseCreator.createSuccessResponse("","评估计算完成");
    }

    /**
     * 计算各烈度下各类建筑物的破坏面积
     * @param evaluateParam
     * @return
     */
    @PostMapping("/countIntensityAndStructureOfArea")
    public RestResponse countIntensityAndStructureOfArea(@RequestBody EvaluateParam evaluateParam) throws Exception {
        RestResponse restResponse = null;
        try {
            System.out.println("人员伤亡评估第一步开始");
            Long start = System.currentTimeMillis();
            if (StringUtils.isBlank(evaluateParam.getPopulationVersion())) {
                throw new RuntimeException("评估失败,请选择人口数据版本");
            }
            if (StringUtils.isBlank(evaluateParam.getBuildingVersion())) {
                throw new RuntimeException("评估失败,请先前往【房屋破坏估计模块】进行评估计算");
            }
            if (StringUtils.isBlank(evaluateParam.getInfluenceVersion()) || StringUtils.isBlank(evaluateParam.getFvId())) {
                throw new RuntimeException("评估失败,请选择影响场数据版本");
            }
             restResponse = casualtiesService.countIntensityAndStructureOfArea(evaluateParam);
            System.out.println("人员伤亡评估第一步结束，花费时间" + ((System.currentTimeMillis() - start) / 1000));
        } catch (Exception e) {
            String errorMessage = "计算失败!";
            log.error(errorMessage,e);
            return RestResponse.fail(e.getMessage());
        }
        return restResponse;
    }

    /**
     * 计算格网下的人员伤亡
     * @param vo
     * @return
     */
    @PostMapping("/savePbeInjuries")
    public RestResponse savePbeInjuries(@RequestBody CalculateVO vo) {
        System.out.println("人员伤亡评估第2步开始");
        Long start = System.currentTimeMillis();
        try {
            if(StringUtils.isBlank(vo.getModelId())){
                throw new RuntimeException("评估失败,请选择评估模型");
            }
            if(StringUtils.isBlank(vo.getXsemId())){
                throw new RuntimeException("评估失败,请选择要评估的地震事件");
            }
            RestResponse restResponse = casualtiesService.savePbeInjuries(vo.getModelId(), vo.getXsemId());
            System.out.println("人员伤亡评估第2步结束，花费时间"+((System.currentTimeMillis()-start)/1000));
            return restResponse;
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询人口和建筑物格网版本列表
     */
    @GetMapping("/getVersion")
    public RestResponse getVersion(SysDataVersionPageParam pageParam ){
        return casualtiesService.getVersion(pageParam);
    }
    /**
     * 选择模型
     */
    @GetMapping("/chooseModel")
    public RestResponse chooseModel(ModelParam modelParam ){
        return casualtiesService.chooseModel(modelParam);
    }

    /**
     * 模型查看
     */
    @GetMapping("/getModelById")
    public RestResponse getModelById(String id ){
        casualtiesService.getModelById(id);
        return casualtiesService.getModelById(id);
    }

    /**
     * 查看人员伤亡评估结果
     * @param  xsemId 事件id
     */
    @GetMapping("/getPopulationResult")
    public RestResponse getPopulationResult(@RequestParam String xsemId ){
        return casualtiesService.getPopulationResult(xsemId);
    }

    @GetMapping("/getModelData")
    public RestResponse getModelData(@RequestParam String xsemId ){
        return casualtiesService.getModelData(xsemId);
    }

    /**
     * 评估使用过的易损性数据查看
     * @return
     */
    @GetMapping("/getAssessmentVulnerability")
    public RestResponse getAssessmentVulnerability(EvaluateParam evaluateParam){
        RestResponse restResponse = null;
        try{
            List<AssessmentVulnerability> list=casualtiesService.getAssessmentVulnerability(evaluateParam);
            restResponse = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查看失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 面积配比保存
     * @return
     */
    @PostMapping("/saveAreaRatio")
    public RestResponse saveAreaRatio(@RequestBody AreaRatio areaRatio){
        RestResponse restResponse = null;
        try{
            casualtiesService.saveAreaRatio(areaRatio);
            restResponse = RestResponse.succeed("保存成功!");
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
}
